//
//  NYSettingCachesCell.m
//  BS
//
//  Created by neal on 15/8/19.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYSettingCachesCell.h"

@interface NYSettingCachesCell ()
/* quanquan */
@property(nonatomic ,weak) UIActivityIndicatorView *loadingView;

@end
@implementation NYSettingCachesCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.textLabel.text = @"清除缓存";
        UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [loadingView startAnimating];
        self.loadingView = loadingView;
        [self.contentView addSubview:loadingView];
        
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        [self getCacheSize];
    }
    return self;
}
//获取缓存文件大小
- (void)getCacheSize
{
    /**
     *  子线程计算文件大小
     */
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
     
        NSString *cachepath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSString *fileSize = [cachepath stringByAppendingPathComponent:@"default/com.hackemist.SDWebImageCache.default"].fileSizeString;
        NSString *text = [NSString stringWithFormat:@"清理缓存（已使用%@）",fileSize];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.textLabel.text = text;
            [self.loadingView removeFromSuperview];
        });
    });
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
- (void)reset
{
    self.textLabel.text = @"清理缓存（0B）";
}
@end
